// dockerregister project main.go
package main

import (
	"encoding/json"
	"fmt"
	"time"
)

//创建全局对象
var mainStatic *MainStatic //存放静态变量结构体

func main() {
	//	go testCheckRestart()

	for {
		//创建资源
		if mainStatic == nil || mainStatic.CHECK_RESTART == true {
			mainStatic = InitResourceMain()
		}
		//监控资源
		mainStatic.WatchResource()

	}

	//test()
}

func testCheckRestart() {
	for {
		time.Sleep(time.Second * 30)
		if mainStatic != nil {
			fmt.Println("=================CLIENT_KEY:" + CLIENT_KEY + "===CHECK_RESTART:")
			fmt.Println(!mainStatic.CHECK_RESTART)
			mainStatic.CHECK_RESTART = !mainStatic.CHECK_RESTART
		}
	}
}

func test() {
	jsonA := `{
    "name": "docker-current",
    "path": "/usr/bin",
    "ref": "root",
    "child": [
        {
            "name": "kube-proxy",
            "path": "/usr/bin",
            "ref": "docker-current",
            "child": [
                {
                    "name": "ref2kube-proxy",
                    "path": "/usr/bin",
                    "ref": "kube-proxy",
                    "child": []
                }
            ]
        },
        {
            "name": "kubelet",
            "path": "/usr/bin",
            "ref": "docker-current",
		
            "child": [
                {
                    "name": "ref2kubelet",
                    "path": "/usr/bin",
                    "ref": "kubelet",
                    "child": []
                }
            ]
        }
    ]
}
`

	var treeMap map[string]interface{}
	if errA := json.Unmarshal([]byte(jsonA), &treeMap); errA != nil {
		panic(errA)
	}

	jsonB := `{"name": "B"}`
	var treeMapB map[string]interface{}
	if errB := json.Unmarshal([]byte(jsonB), &treeMapB); errB != nil {
		panic(errB)
	}

	jsonC := `{"name": "C"}`
	var treeMapC map[string]interface{}
	if errC := json.Unmarshal([]byte(jsonC), &treeMapC); errC != nil {
		panic(errC)
	}

	jsonD := `{"name": "D"}`
	var treeMapD map[string]interface{}
	if errD := json.Unmarshal([]byte(jsonD), &treeMapD); errD != nil {
		panic(errD)
	}

	insertNode(treeMap, "A", treeMapB)
	insertNode(treeMap, "B", treeMapC)
	//insertNode(treeMap, "C", treeMapD)
	str, _ := json.Marshal(treeMap)

	fmt.Println(string(str))
	fmt.Println("\n------------search----------------------\n")
	nodeVal := searchNode(treeMap, "ref2kube-proxy")
	//fmt.Println("\n name:" + nodeVal["name"].(string) + "----path:" + nodeVal["path"].(string) + "---ref:" + nodeVal["ref"].(string))
	printNode(nodeVal)
}
func printNode(treeMap map[string]interface{}) {
	fmt.Println("\n name:" + treeMap["name"].(string) + "----path:" + treeMap["path"].(string) + "---ref:" + treeMap["ref"].(string))
	if treeMap["child"] == nil {
		return
	}

	childData := treeMap["child"].([]interface{})
	for _, km := range childData {
		//递推
		printNode(km.(map[string]interface{}))

	}

}
func searchNode(treeMap map[string]interface{}, nodeName string) map[string]interface{} {

	//fmt.Println("\n name:" + treeMap["name"].(string) + "----path:" + treeMap["path"].(string) + "---ref:" + treeMap["ref"].(string))

	if treeMap["name"] == nodeName {
		return treeMap
	}
	if treeMap["child"] == nil {
		return nil
	}

	childData := treeMap["child"].([]interface{})
	for _, km := range childData {
		//递推
		sn := searchNode(km.(map[string]interface{}), nodeName)
		if sn != nil {
			return sn
		}

	}
	return nil

}

func insertNode(treeMap map[string]interface{}, nodeName string, node map[string]interface{}) bool {
	/*
		if treeMap["name"] == nodeName {
			if treeMap["child"] == nil {
				childData := make(map[string]interface{})
																							AS													                                                                                                                                                        	treeMap["child"] = childData
			}
			childData := treeMap["child"].(map[string]interface{})
			keyname := node["name"].(string)
			childData[keyname] = node
			return true
		}
		if treeMap["child"] == nil {
			return false
		}
		childData := treeMap["child"].(map[string]interface{})
		for _, km := range childData {
			if insertNode(km.(map[string]interface{}), nodeName, node) == true {
				return true
			}

		}
		return false
	*/

	if treeMap["name"] == nodeName {
		if treeMap["child"] == nil {
			childData := make([]interface{}, 0)
			treeMap["child"] = childData
		}
		childData := treeMap["child"].([]interface{})
		treeMap["child"] = append(childData, node)

		//copyArray(childData, node)

		return true
	}
	if treeMap["child"] == nil {
		return false
	}
	childData := treeMap["child"].([]interface{})
	for _, km := range childData {
		//递推
		if insertNode(km.(map[string]interface{}), nodeName, node) == true {
			return true
		}

	}
	return false

}

func copyArray(src []interface{}, val interface{}) []interface{} {
	list := make([]interface{}, len(src)+1)
	for i := 0; i < len(src); i++ {
		list[i] = src[i]
	}
	list[len(src)] = val

	return list

}
